package com.shyt.util;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.shyt.dao.RouterVo;
import com.shyt.dao.SysDepartmentVo;
import com.shyt.dao.SysPermissionVo;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Data
@Slf4j
@NoArgsConstructor
public class MenuUtil {


    //部门
    public static List<SysDepartmentVo> getDepartmentTree(List<SysDepartmentVo> sysDepartmentVos) {

        if (CollectionUtils.isEmpty(sysDepartmentVos)) {
            return null;
        }

        List<SysDepartmentVo> trees = new ArrayList<>();
        for (SysDepartmentVo sysDepartmentVo : sysDepartmentVos) {
            if ("0".equals(sysDepartmentVo.getPid())) {
                trees.add(findDepartmentChildren(sysDepartmentVo, sysDepartmentVos));
            }
        }
        return trees;

    }

    /**
     * 获取子类节点（递归算法）
     *
     * @param father
     * @param sysDepartmentVos 菜单集合
     * @return 子树节点数据
     */
    public static SysDepartmentVo findDepartmentChildren(SysDepartmentVo father, List<SysDepartmentVo> sysDepartmentVos) {
        for (SysDepartmentVo children : sysDepartmentVos) {
            if (father.getId().equals(children.getPid())) {
                father.getChildren().add(findDepartmentChildren(children, sysDepartmentVos));
            }
        }
        return father;
    }


    public static List<SysPermissionVo> getMenu(List<SysPermissionVo> sysPermissionVos) {

        if (CollectionUtils.isEmpty(sysPermissionVos)) {
            return null;
        }
        sysPermissionVos = sysPermissionVos.stream().filter(
                item -> (!"2".equals(String.valueOf(item.getType())))).collect(Collectors.toList());
        List<SysPermissionVo> trees = new ArrayList<>();
        for (SysPermissionVo sysPermissionVo : sysPermissionVos) {
            if (0 == sysPermissionVo.getParentId()) {
                trees.add(findChildren(sysPermissionVo, sysPermissionVos));
            }
        }
        return trees;

    }

    /**
     * 获取子类节点（递归算法）
     *
     * @param father
     * @param sysPermissionVos 菜单集合
     * @return 子树节点数据
     */
    public static SysPermissionVo findChildren(SysPermissionVo father, List<SysPermissionVo> sysPermissionVos) {
        for (SysPermissionVo children : sysPermissionVos) {
            if (father.getId().equals(children.getParentId())) {
                father.getChildren().add(findChildren(children, sysPermissionVos));
            }
        }
        return father;
    }


    public static List<RouterVo> makeRouter(List<SysPermissionVo> sysPermissionVos, long id) {
        ArrayList<RouterVo> routerVos = new ArrayList<>();

        if (CollectionUtils.isEmpty(sysPermissionVos)) {
            return routerVos;
        }

        List<SysPermissionVo> finalSysPermissionVos = sysPermissionVos;
        sysPermissionVos.stream().filter(item -> item.getParentId() == id).forEach(item -> {

            RouterVo routerVo = new RouterVo();
            routerVo.setPath(item.getPath());
            routerVo.setName(item.getName());

            if (item.getParentId() == 0l) {
                routerVo.setComponent("layOut");
                routerVo.setAlwaysShow(true);
            } else {
                routerVo.setComponent(item.getUrl());
                routerVo.setAlwaysShow(false);
            }

            routerVo.setMeta(routerVo.new Meta(item.getLabel(),
                    item.getIcon(),
                    item.getCode().split(",")));

            List<RouterVo> child = makeRouter(finalSysPermissionVos, item.getId());
            routerVo.setChildren(child);
            routerVos.add(routerVo);

        });

        return routerVos;
    }


}
